package com.example.zhu95.mytimber.adapters;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.activity.BaseActivity;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.listener.PopupMenuItemClickListener;
import com.example.zhu95.mytimber.ui.MusicLinearClip;
import com.example.zhu95.mytimber.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.zhu95.mytimber.ConstantValue.MusicState_Paused;
import static com.example.zhu95.mytimber.ConstantValue.MusicState_Playing;
import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd_Play;
import static com.example.zhu95.mytimber.activity.BaseActivity.state;

/**
 * @author zhu95 on 2017/12/19.
 */

public class ItemVdragAdapter extends RecyclerView.Adapter<ItemVdragAdapter.ViewHolder> {
    protected List<SongBean> songs;
    protected ItemTouchHelper itemTouchHelper;
    protected int itemColor = Color.BLACK;
    protected BaseActivity activity;

    public int getItemColor() {
        return itemColor;
    }

    public void setItemColor(int color) {
        if (color == Color.WHITE) {
            itemColor = color;
        } else {
            itemColor = Color.BLACK;
        }
    }

    public ItemVdragAdapter(List<SongBean> songs, BaseActivity activity) {
        if (songs == null) {
            songs = new ArrayList<>();
        }
        this.songs = songs;
        this.activity = activity;
    }

    public List<SongBean> getSongs() {
        return songs;
    }

    public void setSongs(List<SongBean> songs) {
        this.songs = songs;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_vdrag, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (itemColor == Color.WHITE) {
            holder.imgDrag.setImageResource(R.drawable.drag_10point_white);
            holder.songMoreVert.setImageResource(R.drawable.ic_more_vert_white_24dp);
            holder.tvArtist.setTextColor(itemColor);
        }
        holder.buttonLineSongs.setVisibility(View.GONE);
        holder.songItemLayout.setClickable(false);
        final SongBean song = songs.get(position);
        if (song.getId() == BaseActivity.songid) {
            holder.tvSong.setTextColor(MyApplication.getContext().getResources().getColor(R.color.colorAccent));
            if (TextUtils.equals(state, MusicState_Playing)) {
                holder.musicLinearClip.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals(state, MusicState_Paused)) {
                holder.musicLinearClip.setVisibility(View.GONE);
            }
        } else {
            holder.tvSong.setTextColor(itemColor);
            holder.musicLinearClip.setVisibility(View.GONE);
        }
        holder.tvArtist.setText(song.getArtist());
        holder.tvSong.setText(song.getTitle());
        String albumArt = song.getAlbumArt();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(holder.itemView.getContext()).load(albumArt).centerCrop().into(holder.imageArtist);
        }

        setEventListener(holder, song);
    }

    private void setEventListener(final ViewHolder holder, final SongBean song) {
        setPopupMenu(holder, song.getId());
        // MARK:2017/12/20 设置触摸事件启动item的拖拽
        holder.imgDrag.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (itemTouchHelper != null) {
                    itemTouchHelper.startDrag(holder);
                }
                return true;
            }
        });
        holder.songItemLayoutDrag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.sendPlayMusicCmdBroadcast(song.getId(), Util.INSTANCE.getIdsStrFormat(songs), PlayMusicCmd_Play);
            }
        });
    }

    protected void setPopupMenu(ViewHolder holder, final int songid) {
        holder.songMoreVert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
                Menu menu = popupMenu.getMenu();
                popupMenu.getMenuInflater().inflate(R.menu.song_more_popup, menu);
                menu.findItem(R.id.song_play_next).setVisible(false);
                menu.findItem(R.id.song_add2queue).setVisible(false);
                menu.findItem(R.id.song_share).setVisible(false);
                menu.findItem(R.id.song_delete).setVisible(false);
                popupMenu.setOnMenuItemClickListener(
                        new PopupMenuItemClickListener(activity, songid, Util.INSTANCE.getIdsStrFormat(songs), ItemVdragAdapter.this)
                );
                popupMenu.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return songs.size();
    }

    public void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
        this.itemTouchHelper = itemTouchHelper;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_drag)
        public ImageView imgDrag;
        @BindView(R.id.image_artist)
        ImageView imageArtist;
        @BindView(R.id.tv_song)
        TextView tvSong;
        @BindView(R.id.tv_itemInfo)
        TextView tvArtist;
        @BindView(R.id.more_vert)
        ImageView songMoreVert;
        @BindView(R.id.play_sign)
        MusicLinearClip musicLinearClip;
        @BindView(R.id.button_line_songs)
        View buttonLineSongs;
        @BindView(R.id.song_item_layout)
        RelativeLayout songItemLayout;
        @BindView(R.id.song_item_layout_vdrag)
        LinearLayout songItemLayoutDrag;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public ImageView getSongMoreVert() {
            return songMoreVert;
        }
    }

}
